﻿using HLStock.Common;
using HLStock.DBAccess;
using HLStock.MyType;
using HLStock.MyType.FieldType;
using Newtonsoft.Json;

namespace HLStock.WmsApi
{

    public class MyRefJsonConveter : JsonConverter
    {
        MyForeignAttribute foreignAttribute;
        public MyRefJsonConveter(MyForeignAttribute foreignAttribute)
        {
            this.foreignAttribute = foreignAttribute;
            refType = foreignAttribute.RefType;
        }

        Type refType;
        public override bool CanRead => false;
        public override bool CanConvert(Type objectType)
        {
            return foreignAttribute != null && foreignAttribute.LabelProp != null;
        }

        public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteValue(new object());
            }
            else
            {
                var dbFuzzyQuery = new DbFuzzyQuery(ServiceLoader.GetService<DbInstance>(), foreignAttribute.RefType);
                Object result = null;
                Object result2 = null;
                Object obj;
                if (foreignAttribute.LabelProp != null)
                    result = dbFuzzyQuery.GetField(foreignAttribute.LabelProp, value!.ToString()!)!;
                if (foreignAttribute.LabelProp2 != null)
                    result2 = dbFuzzyQuery.GetField(foreignAttribute.LabelProp2, value!.ToString()!)!;
                obj = new { _k = value.ToString(), _v1 = result == null ? result2 : result, _v2 = result == null ? null : result2 };// _ new { key = value, label = result };
                serializer.Serialize(writer, obj);
            }
        }
    }
    public class MyBooleanJsonConveter : JsonConverter<MyBoolean>
    {
        public override bool CanRead => false;

        public override MyBoolean? ReadJson(JsonReader reader, Type objectType, MyBoolean? existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, MyBoolean? value, JsonSerializer serializer)
        {
            writer.WriteValue(value?.Val);
        }
    }
    public class MyDateTimeJsonConveter : JsonConverter<MyDateTime>
    {
        public override bool CanRead => false;

        public override MyDateTime? ReadJson(JsonReader reader, Type objectType, MyDateTime? existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, MyDateTime? value, JsonSerializer serializer)
        {
            var jsonstr = value?.ToString();
            writer.WriteValue(jsonstr);
        }
    }
}

